﻿Imports System.Xml
Imports System.IO
Public Class XML_worker

    Public Shared Function createXmlSettingsFile(ByVal dbGenSett As DBGenSettings) As Boolean
        Dim writer As XmlTextWriter = Nothing

        writer = New XmlTextWriter(dbGenSett.SavePath, Nothing)
        'Use indentation for readability.
        writer.Formatting = Formatting.Indented
        writer.Indentation = 4
        'writer.WriteComment("Commentss: Settings for DB Generator")
        'writer.WriteProcessingInstruction("Instruction", "DB generator Settings")
        writer.WriteStartElement("settings")
        'Server 
        writer.WriteStartElement("server")
        writer.WriteString(dbGenSett.ServerName)
        writer.WriteEndElement()
        'Database
        writer.WriteStartElement("databaseName")
        writer.WriteString(dbGenSett.DbName)
        writer.WriteEndElement()
        'Database
        writer.WriteStartElement("databaseFileName")
        writer.WriteString(dbGenSett.DBFileName)
        writer.WriteEndElement()
        'Version
        writer.WriteStartElement("version")
        writer.WriteString(dbGenSett.Ver)
        writer.WriteEndElement()
        'system's sql system script 
        writer.WriteStartElement("systemScripts")
        writer.WriteString(dbGenSett.SystSQLscriptsPath)
        writer.WriteEndElement()
        'db path where to save generated DB
        writer.WriteStartElement("dbPath")
        writer.WriteString(dbGenSett.DbPath)
        writer.WriteEndElement()
        writer.WriteEndElement()
        writer.Close()

        Return True
    End Function

    Public Shared Function openDBGenSettings(ByVal settingsFilePath As String) As DBGenSettings
        Dim reader As XmlTextReader = New XmlTextReader(settingsFilePath)
        Dim DBGeneratorSettings As DBGenSettings = Nothing
        Dim server As String = ""
        Dim databaseName As String = ""
        Dim SystemScripts As String = ""
        Dim version As String = ""
        Dim dbPath As String = ""
        Dim dbFileName As String = ""
        Do While (reader.Read())

            If (reader.NodeType = XmlNodeType.Element) Then
                'reader.Read()

                Select Case reader.Name
                    Case "server"
                        server = reader.ReadString().ToString()
                    Case "databaseName"
                        databaseName = reader.ReadString().ToString()
                    Case "databaseFileName"
                        dbFileName = reader.ReadString().ToString()
                    Case "version"
                        version = reader.ReadString().ToString()
                    Case "systemScripts"
                        SystemScripts = reader.ReadString().ToString()
                    Case "dbPath"
                        dbPath = reader.ReadString().ToString()
                End Select
            End If
        Loop
        If ((dbFileName = Nothing) Or (server = Nothing) Or (databaseName = Nothing) Or (version = Nothing) Or (dbPath = Nothing)) Then
            Throw New Exception("CHYBA - nepodarilo sa načítať všetky potrebne nastavenia.")
        End If

        reader.Close()
        Return New DBGenSettings(dbFileName, server, databaseName, Convert.ToInt32(version), SystemScripts, dbPath, settingsFilePath)
    End Function

End Class
